package com.spring.component.job.support;

import com.spring.component.utils.CollectionUtil;
import com.spring.component.utils.CommonUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;

import java.util.SplittableRandom;

/**
 * @author liushihua
 * @since 2018年04月28日
 */
public abstract class AbstractJob implements Job {

  public static final String JOB_NAME_PREFIX = "component_job_name_";
  public static final String JOB_GROUP_NAME_PREFIX = "component_job_group_name_";

  private JobDataMap jobDataMap;

  public abstract String getName();

  public abstract String getGroupName();

  public abstract String getCronExpression();

  protected String getDefaultName() {
    return JOB_NAME_PREFIX + CommonUtils.uuid();
  }

  protected String getDefaultGroupName() {
    return JOB_GROUP_NAME_PREFIX + CommonUtils.uuid();
  }

  public void setJobDataMap(JobDataMap jobDataMap) {
    this.jobDataMap = jobDataMap;
  }

  public JobDataMap getJobDataMap() {
    return this.jobDataMap != null ? this.jobDataMap : new JobDataMap();
  }

  public Object getAttribute(String name) {
    return CollectionUtil.isBlank(jobDataMap) ? null : jobDataMap.get(name);
  }

  public void addAttribute(String name, Object value) {
    if (null != jobDataMap) {
      jobDataMap.put(name, value);
    }
  }
}
